package com.macro.mall.portal.controller;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.annotation.AccessLimit;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.wechat.UserPhoneInfo;
import com.macro.mall.portal.domain.wechat.WeChatResult;
import com.macro.mall.portal.service.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author：郎
 * @name：WeChatController
 * @date：2023/12/18 13:08
 */
@RestController
@Api(tags = "WeChatController")
@Tag(name = "WeChatController", description = "微信相关接口")
@RequestMapping("/weChat")
@AllArgsConstructor
public class WeChatController {
    private final WeChatService weChatService;

    @AccessLimit
    @ApiOperation("获取sessionKey")
    @RequestMapping(value = "/code2session", method = RequestMethod.GET)
    public CommonResult<Object> code2session(@RequestParam("code") String code) {
        WeChatResult weChatResult = weChatService.code2session(code);
        if (StrUtil.isNotBlank(weChatResult.getOpenid())) {
            return CommonResult.success(weChatResult);
        }
        return CommonResult.failed("获取session失败：" + weChatResult.getErrmsg());
    }

    @AccessLimit
    @ApiOperation("获取phoneNumber")
    @RequestMapping(value = "/phoneNumber", method = RequestMethod.GET)
    public CommonResult<Object> phoneNumber(@RequestParam("code") String code) {
        UserPhoneInfo userPhoneNumber = weChatService.getUserPhoneNumber(code);
        if ("ok".equals(userPhoneNumber.getErrmsg())) {
            UserPhoneInfo.PhoneInfo phoneInfo = userPhoneNumber.getPhoneInfo();
            return CommonResult.success(phoneInfo.getPhoneNumber());
        }
        return CommonResult.failed("获取手机号失败：" + userPhoneNumber.getErrmsg());
    }

}
